package cn.wjssl.dynamic.domain;

import cn.wjssl.dynamic.domain.model.entity.ThreadPoolConfigEntity;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author：lxd
 * @Description
 * @Date：2025/3/22
 */
@Slf4j
public class DynamicThreadPoolService implements IDynamicThreadPoolService {

    private final String applicationName;

    private final Map<String, ThreadPoolExecutor> threadPoolExecutorMap;

    public DynamicThreadPoolService(String applicationName, Map<String, ThreadPoolExecutor> threadPoolExecutorMap) {
        this.applicationName = applicationName;
        this.threadPoolExecutorMap = threadPoolExecutorMap;
    }

    // 获取所有线程池配置的列表
    @Override
    public List<ThreadPoolConfigEntity> queryThreadPoolList() {

        List<ThreadPoolConfigEntity> threadPoolConfigEntities = new ArrayList<>(threadPoolExecutorMap.size());
        threadPoolExecutorMap.forEach((key, value) -> {
            ThreadPoolConfigEntity threadPoolConfigEntity = new ThreadPoolConfigEntity(this.applicationName, key);
            threadPoolConfigEntity.setCorePoolSize(value.getCorePoolSize());
            threadPoolConfigEntity.setMaximumPoolSize(value.getMaximumPoolSize());
            threadPoolConfigEntity.setActiveCount(value.getActiveCount());
            threadPoolConfigEntity.setPoolSize(value.getPoolSize());
            threadPoolConfigEntity.setQueueType(value.getQueue().getClass().getSimpleName());
            threadPoolConfigEntity.setQueueSize(value.getQueue().size());
            threadPoolConfigEntity.setRemainingCapacity(value.getQueue().remainingCapacity());
            threadPoolConfigEntities.add(threadPoolConfigEntity);
        });
        return threadPoolConfigEntities;
    }

    // 根据线程池的bean名字, 获取对应的线程池
    @Override
    public ThreadPoolConfigEntity queryThreadPoolConfigByName(String threadPoolName) {
        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolName);
        if (threadPoolExecutor == null) {return new ThreadPoolConfigEntity(this.applicationName, threadPoolName);}

        // 线程池配置数据
        ThreadPoolConfigEntity threadPoolConfigEntity = new ThreadPoolConfigEntity(applicationName, threadPoolName);
        threadPoolConfigEntity.setCorePoolSize(threadPoolExecutor.getCorePoolSize());
        threadPoolConfigEntity.setMaximumPoolSize(threadPoolExecutor.getMaximumPoolSize());
        threadPoolConfigEntity.setActiveCount(threadPoolExecutor.getActiveCount());
        threadPoolConfigEntity.setPoolSize(threadPoolExecutor.getPoolSize());
        threadPoolConfigEntity.setQueueType(threadPoolExecutor.getQueue().getClass().getSimpleName());
        threadPoolConfigEntity.setQueueSize(threadPoolExecutor.getQueue().size());
        threadPoolConfigEntity.setRemainingCapacity(threadPoolExecutor.getQueue().remainingCapacity());

        log.info("动态线程池，配置查询 应用名:{} 线程名:{} 池化配置:{}", applicationName, threadPoolName, JSON.toJSONString(threadPoolConfigEntity));
        return threadPoolConfigEntity;
    }

    // 更新线程池配置
    @Override
    public void updateThreadPoolConfig(ThreadPoolConfigEntity threadPoolConfigEntity) {
        if (null == threadPoolConfigEntity || !applicationName.equals(threadPoolConfigEntity.getAppName())) return;
        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolConfigEntity.getThreadPoolName());
        if (null == threadPoolExecutor) return;

        threadPoolExecutor.setCorePoolSize(threadPoolConfigEntity.getCorePoolSize());
        threadPoolExecutor.setMaximumPoolSize(threadPoolConfigEntity.getMaximumPoolSize());
    }
}
